package org.jrad.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;

import org.jrad.model.Attribute;
import org.jrad.model.EntityCrud;

public class JSFHelper {

	public static void createWebContent(String inStrDir, String outStrDir) {
		File inDir = new File(inStrDir);
		File outDir = new File(outStrDir);
		if (!outDir.exists()) {
			outDir.mkdir();
		}
		FileFilter filesFilter = new FileFilter() {

			public boolean accept(File f) {
				return !f.isDirectory();
			}
		};
		File[] files = inDir.listFiles(filesFilter);
		for (int i = 0; i < files.length; i++) {
			try {
				System.out.println(files[i].getName());
				copyFiles(files[i], outStrDir);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		FileFilter dirs = new FileFilter() {
			public boolean accept(File f) {
				return f.isDirectory();
			}
		};
		File[] subDirs = inDir.listFiles(dirs);
		for (int i = 0; i < subDirs.length; i++) {
			if (!outDir.getName().equals(subDirs[i].getName())) {
				createWebContent(subDirs[i].toString(), outStrDir + File.separator + subDirs[i].getName());
			}
		}
	}

	public static void copyFiles(File fIn, String fOut) throws IOException {
		FileInputStream fis = new FileInputStream(fIn);
		FileOutputStream fos = new FileOutputStream(fOut + "\\" + fIn.getName());
		FileChannel fcIn = fis.getChannel();
		FileChannel fcOut = fos.getChannel();
		fcOut.transferFrom(fcIn, 0, fcIn.size());
		fos.close();
		fis.close();
	}
	
	public static void changeTemplates(String outStrDir, String appName, List<EntityCrud> cruds) throws FileNotFoundException, IOException{
		String webXml = outStrDir + File.separator + "WEB-INF" + File.separator + "web.xml";
		String defaultX = outStrDir + File.separator + "templates" + File.separator + "default.xhtml";
		String header = outStrDir + File.separator + "pages" + File.separator + "commons"+ File.separator + "header.xhtml";
		String crud = outStrDir + File.separator + "templates" + File.separator + "crud.xhtml";
		String entityAttribute = outStrDir + File.separator + "templates" + File.separator + "entityAttribute.xhtml";
		String entityAttributeList = outStrDir + File.separator + "templates" + File.separator + "entityAttributeList.xhtml";
		String menuItem = outStrDir + File.separator + "templates" + File.separator + "menuItem.xhtml";
		String pages = outStrDir + File.separator + "pages" + File.separator;
		String index = outStrDir + File.separator + "index.jsp";
		
		String fileInputData = JRadHelper.readFile(webXml);		
		fileInputData = fileInputData.replaceAll("#APP_NAME#" , appName);
		JRadHelper.writeFile(webXml, fileInputData);
		
		fileInputData = JRadHelper.readFile(defaultX);		
		fileInputData = fileInputData.replaceAll("#APP_NAME#" , appName);
		JRadHelper.writeFile(defaultX, fileInputData);
		
		String menuItens = "";		
		for (EntityCrud entityCrud : cruds) {			
			String entityAttributes = "";
			String entityAttributeListAll = "";
			String menuItem_ = JRadHelper.readFile(menuItem);
			menuItem_ = menuItem_.replaceAll("#EntityName#" , entityCrud.getName());
			menuItens += menuItem_+"\n";
			for (Attribute att : entityCrud.getAttributes()) {
				String entityAttribute_ = JRadHelper.readFile(entityAttribute);				
				entityAttribute_ = entityAttribute_.replaceAll("#EntityAttribute#" , att.getName());
				entityAttribute_ = entityAttribute_.replaceAll("#EntityName#" , entityCrud.getName().toLowerCase());
				entityAttribute_ = entityAttribute_.replaceAll("#required#" , "required=\""+att.isRequired()+"\"");
				
				String entityAttributeList_ = JRadHelper.readFile(entityAttributeList);
				entityAttributeList_ = entityAttributeList_.replaceAll("#EntityAttribute#" , att.getName());
				entityAttributeList_ = entityAttributeList_.replaceAll("#EntityName#" , entityCrud.getName().toLowerCase());
				
				entityAttributes += entityAttribute_+"\n";
				entityAttributeListAll += entityAttributeList_+"\n";
			}
			
			String crud_ = JRadHelper.readFile(crud);		
			crud_ = crud_.replaceAll("#EntityName#" , entityCrud.getName().toLowerCase());
			crud_ = crud_.replaceAll("#EntityName1UC#" , entityCrud.getName());
			crud_ = crud_.replaceAll("#EntityAttributes#" , entityAttributes);
			crud_ = crud_.replaceAll("#EntityAttributeListAll#" , entityAttributeListAll);
			JRadHelper.writeFile(pages+"crud"+entityCrud.getName() + ".xhtml",crud_);
		}
		fileInputData = JRadHelper.readFile(header);		
		fileInputData = fileInputData.replaceAll("#MenuItem#" , menuItens);
		JRadHelper.writeFile(header, fileInputData);
		
		fileInputData = JRadHelper.readFile(index);		
		fileInputData = fileInputData.replaceAll("#FirstEntity#" , cruds.get(0).getName());
		JRadHelper.writeFile(index, fileInputData);
	}
	

//	public static void main(String[] args) {
//		createWebContent("template" + File.separator + "WebContent", "c:\\jRAD\\WebContent");
//		
//		try {
//			//FileChannel fcg = new RandomAccessFile(new File("template"+File.separator+), "rw").getChannel();
//			 
//		}
//		catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
}
